package com.sinodata.bsm.cicp.collector.informix;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 由于Informix采集指标的修改，该采集类放弃使用
 * 
 * <p>
 * Description: 日志缓冲区的使用情况采集器
 * 
 * 物理日志缓存区：
 *     如pages/io除以bufsize大约为75％，则可以判定缓冲区的使用效率较高。
 *     如果小于75％，则物理日志缓冲区可能太大。
 *     如果该比率大于90％。则物理缓冲区太小。 
 * 逻辑日志缓存区：
 *     采取同物理日志缓存区一样的方法，监视逻辑日志缓冲区的使用情况
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-18 PM 6:38:13    wangpeng       1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class LogBufferedCollector extends Collector {
    private static Logger logger = Logger.getLogger(LogBufferedCollector.class);

    @Override
    public String[] getPropMeta() {
        return new String[] { "PhysicalBufSiz", "PhysicalPagesIO", "LogicalBufSiz", "LogicalPagesIO" };
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "cmd" };
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String[] propMetas = this.getPropMeta();
        String cmd = this.getParamMap().get("cmd");
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        ResPropertyValue resPropertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] props = new ResPropertyValue.PropValue[propMetas.length];
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, cmd, 0, 0);
            String strLine = null;
            double physicalBufSiz = 0;
            double physicalPagesIO = 0;
            double logicalBufSiz = 0;
            double logicalPagesIO = 0;
            while ((strLine = reader.readLine()) != null) {
                if (strLine.contains("Physical Logging")) {
                    reader.readLine();
                    strLine = reader.readLine().trim();
                    String[] str = strLine.split("\\s+");
                    physicalBufSiz = Double.parseDouble(str[2]);
                    physicalPagesIO = Double.parseDouble(str[5]);
                } else if (strLine.contains("Logical Logging")) {
                    reader.readLine();
                    strLine = reader.readLine().trim();
                    String[] str = strLine.split("\\s+");
                    logicalBufSiz = Double.parseDouble(str[2]);
                    logicalPagesIO = Double.parseDouble(str[7]);
                }
            }
            props[0] = ResPropertyValue.createPropValue(physicalBufSiz + "", propMetas[0]);
            props[1] = ResPropertyValue.createPropValue(physicalPagesIO + "", propMetas[1]);
            props[2] = ResPropertyValue.createPropValue(logicalBufSiz + "", propMetas[2]);
            props[3] = ResPropertyValue.createPropValue(logicalPagesIO + "", propMetas[3]);
            resPropertyValueX.setValues(props);
            values.add(resPropertyValueX);
        } catch (IOException e) {
            logger.error("failed to collect informix logbuf", e);
            return null;
        }
        return values;
    }

}
